/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChatWindow.java
 *
 * Created on May 15, 2011, 6:40:55 PM
 */
package securedchatui;

import controller.ClientController;
import java.util.ArrayList;
import javax.swing.AbstractListModel;
import org.jdesktop.application.Action;

/**
 *
 * @author ahmed
 */
public class ChatWindow extends javax.swing.JFrame {

    private ClientController controller;

    /** Creates new form ChatWindow */
    public ChatWindow() {
        initComponents();
        controller = ClientController.getInstance(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(securedchatui.SecuredChatUIApp.class).getContext().getResourceMap(ChatWindow.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setModel(chatMessages);
        jList1.setName("jList1"); // NOI18N
        jScrollPane1.setViewportView(jList1);

        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(securedchatui.SecuredChatUIApp.class).getContext().getActionMap(ChatWindow.class, this);
        jButton1.setAction(actionMap.get("sendNewMessage")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jList2.setModel(loginUsers);
        jList2.setName("jList2"); // NOI18N
        jScrollPane2.setViewportView(jList2);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 475, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 547, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                    .addComponent(jButton1))
                .addGap(5, 5, 5)
                .addComponent(jLabel1))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    MyListModel chatMessages = new MyListModel();
    private javax.swing.JList jList2;
    MyListModel loginUsers = new MyListModel();
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JMenuBar menuBar;
    // End of variables declaration//GEN-END:variables
    private String ipAddress;
    private int port;
    private int mode;
    private String username;
    private String password;
    public static final int SYMMETRIC = 1 << 1;
    public static final int ASSYMMETRIC = 1 << 2;

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    

    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    
    public void setMode(int mode) {
        this.mode = mode;
    }

    
    public void setPort(String port) {
        this.port = Integer.parseInt(port);
    }
    
    /**
     * Add a user to Room user's lists
     * @param username username of the new user
     */
    public void addUser(String username)
    {
        loginUsers.add(username);
        jList2.setListData(loginUsers.strings.toArray());
    }
    
    /**
     * Add group of users to Room user's list
     * @param usernames list of usernames of new users
     */
    public void addUsers(String[] usernames)
    {
        for (String user: usernames) {
            loginUsers.add(user);
        }
        jList2.setListData(loginUsers.strings.toArray());
    }
    
    /**
     * remove user when it's disconnected from chat
     * @param username disconnected username
     */
    public void removeUser(String username)
    {
        loginUsers.delete(username);
        jList2.setListData(loginUsers.strings.toArray());
    }
    
    /**
     * Add message to chat
     * @param message new message
     */
    public void addMessage(String message)
    {
        chatMessages.add(message);
        jList1.setListData(chatMessages.strings.toArray());
    }
    
    /**
     * add group of messages to chat
     * @param messages new messages
     */
    public void addMessages(String[] messages)
    {
        for (String msg: messages) {
            loginUsers.add(msg);
        }
        jList1.setListData(chatMessages.strings.toArray());
    }

    @Action
    public void sendNewMessage() {
        String  text = jTextField1.getText();
        if (text.length() > 0)
            controller.sendMessage(text);
    }

    public void initializeClient()
    {
        controller.connectToServer(ipAddress, port, username, password);
    }
}

class MyListModel extends AbstractListModel{
    ArrayList<String> strings = new ArrayList<String>();
    public int getSize() { return strings.size(); }
    public Object getElementAt(int i) { return strings.get(i); }
    public void add(String str) { strings.add(str); }
    public void delete(String str) { strings.remove(str);}
}